/*
 * GUID.java
 *
 * Created on September 6, 2006, 10:40 AM
 *
 */


package edu.byu.isys413.mystuff.sales;
import java.lang.*;
import java.net.*;

/**
 * Generates globally-unique identifiers
 *
 * @author westinhatch, <westin@byu.edu>, ISys 403, Section 1
 * @version 1.0
 */
public class GUID {
    //I declare needed variables that I will use throughout class
    private static String savedGuid = "";
    private static long counter = 0;
    private static String guid = "";
    /** Creates a new instance of GUID */
    private GUID() {
    }
    
    /**
     *Generates a new GUID
     *
     *@ returns a unique GUID as a string
     */
    public static synchronized String generate() throws Exception {
     /*Get current time in milliseconds and convert it to hex
      */
        //This makes sure that the next GUID created is tested against
        //the old GUID which is now stored
        if (savedGuid.equals(Long.toHexString(System.currentTimeMillis()))){
            //If it hit the max I will sleep the program and then set guid
            //and counter
            if (counter >= 16777216){
                Thread.sleep(1);
                guid = Long.toHexString(System.currentTimeMillis());
                savedGuid = guid;
                counter = 0;
            //If the counter has not hit the max I will add to the counter
            } else counter ++;
        //If the current time and old guid's time are not the same then
            //I get the new time and reset the counter
        } else {
            guid = Long.toHexString(System.currentTimeMillis());
            savedGuid = guid;
            counter = 0;
        }
        
        
        
//This for loop is for padding 0s
        for ( boolean test = true; test == true;){
            if (guid.length() > 15){
                test = false;
            } else guid = "0" + guid;
        }
        
        
        //**This next part just converts the counter to hex in a "mid"dle-man
        //variable and pads the 0s so it is 6 digits
        String mid = Long.toHexString(counter);
        for ( boolean test = true; test == true;){
            if (mid.length() > 5){
                test = false;
            } else mid = "0" + mid;
        }
        //GUID is extended to include the padded counter
        String endGuid = guid + mid;
        //Now I get the ip address in byte
        InetAddress net = InetAddress.getLocalHost();
        byte[] ip = net.getAddress();
        long [] finalIp = {0,0,0,0};
        for (int i = 0; i < ip.length; i++){
            //**Converts byte to long
            finalIp[i] = ip[i];
        }
        for (int i = 0; i < finalIp.length; i++){
            //**Converts negative numbers resulting from the byte format
            //back to their actual ip number
            if (finalIp[i] < 0){
                finalIp[i] = finalIp[i] + 256;
            }
            //**Converts the ip # to hex and pads it if needed
            if (finalIp[i] < 17){
                endGuid = endGuid + "0" + Long.toHexString(finalIp[i]);
            } else endGuid = endGuid + Long.toHexString(finalIp[i]);
        }
        //returns complete GUID to main
        return endGuid;
    }
}









